package com.jugheadzhou.auth.core.message.consumer.config;

import com.jugheadzhou.auth.core.common.utils.JsonUtil;
import com.jugheadzhou.auth.core.message.consumer.handler.ReceiverListenerDefaultErrorHandler;
import com.jugheadzhou.auth.core.message.consumer.receiver.WsGlobalConDlxReceiver;
import com.jugheadzhou.auth.core.message.consumer.receiver.WsGlobalConReceiver;
import com.jugheadzhou.auth.service.UserService;
import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpoint;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory;

/**
 * 接收消息配置
 *
 * @author jugheadzhou
 * @date 2021-12-05
 */
@Configuration
public class RabbitConsumerConfig implements RabbitListenerConfigurer {
    /**
     * Callback allowing a {@link RabbitListenerEndpointRegistry
     * RabbitListenerEndpointRegistry} and specific {@link RabbitListenerEndpoint
     * RabbitListenerEndpoint} instances to be registered against the given
     * {@link RabbitListenerEndpointRegistrar}. The default
     * {@link RabbitListenerContainerFactory RabbitListenerContainerFactory}
     * can also be customized.
     *
     * @param registrar the registrar to be configured
     */
    @Override
    public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
        registrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory());
    }

    @Bean
    public MessageHandlerMethodFactory messageHandlerMethodFactory() {
        DefaultMessageHandlerMethodFactory messageHandlerMethodFactory = new DefaultMessageHandlerMethodFactory();
        messageHandlerMethodFactory.setMessageConverter(consumerJackson2MessageConverter());
        return messageHandlerMethodFactory;
    }

    /**
     * 配置Jackson进行消息转换
     * @return
     */
    @Bean
    public MappingJackson2MessageConverter consumerJackson2MessageConverter() {
        MappingJackson2MessageConverter mappingJackson2MessageConverter = new MappingJackson2MessageConverter();
        // 自定义objectMapper 主要针对java8的时间API进行兼容
        mappingJackson2MessageConverter.setObjectMapper(JsonUtil.getObjectMapper());
        return mappingJackson2MessageConverter;
    }

    /**
     * 监听
     * @return
     */
    @Bean
    @ConditionalOnBean(UserService.class)
    public WsGlobalConReceiver wsGlobalConReceiver(UserService userService){
        return new WsGlobalConReceiver(userService);
    }
    /**
     * 监听
     * @return
     */
    @Bean
    public WsGlobalConDlxReceiver wsGlobalConDlxReceiver(){
        return new WsGlobalConDlxReceiver();
    }

    /**
     * 监听异常处理
     * @return
     */
    @Bean("receiverListenerErrorHandler")
    public ReceiverListenerDefaultErrorHandler receiverListenerErrorHandler(){
        return new ReceiverListenerDefaultErrorHandler();
    }

}
